/**
 * 这里仅放子工程的 util 代码
 */
import {
	detailField as purchaseDetail
} from "@/views/superviseInfo/js/purchase-constant";
import {
	detailField as subjectDetail
} from "@/views/superviseInfo/js/subject-constant";
import {
	detailField as offerDetail
} from "@/views/superviseInfo/js/offer-constant";
import {
	detailField as contractDetail
} from "@/views/superviseInfo/js/contract-constant";
import {
	detailField as buyerDetail
} from "@/views/superviseInfo/js/buyer-constant";
import {
	detailField as sellerDetail
} from "@/views/superviseInfo/js/seller-constant";
import {
	detailField as performanceDetail
} from "@/views/superviseInfo/js/performance-constant";
import {
	detailField as invoiceDetail
} from "@/views/superviseInfo/js/invoice-payment";
import Vue from "vue";
import detailInfo from "@/components/detailInfo";
import {
	superviseInfoAPI
} from "@api/superviseInfo";
import detailPage from "@/components/detailInfo";
import {
	off
} from "element-ui";

export const mapTable = {
	"purchase-info": {
		editUrl: "purchaser/edit",
		submitUrl: "purchaser/submit",
		historyDetailUrl: 'purchaser/history/detail',
		detailUrl: 'purchaser/detail',
		detailField: purchaseDetail,
		path: 'purchaser'
	},
	"subject-info": {
		editUrl: "bid/edit",
		submitUrl: "bid/submit",
		historyDetailUrl: 'bid/history/detail',
		detailUrl: 'bid/detail',
		detailField: subjectDetail,
		path: 'bid'
	},
	"offer-supplier": {
		editUrl: "supplier/edit",
		submitUrl: "supplier/submit",
		historyDetailUrl: 'supplier/history/detail',
		detailUrl: 'supplier/detail',
		detailField: offerDetail,
		path: 'supplier'
	},
	"contract-info": {
		editUrl: "contract/edit",
		submitUrl: "contract/submit",
		historyDetailUrl: 'contract/history/detail',
		detailUrl: 'contract/detail',
		detailField: contractDetail,
		path: 'contract'
	},
	"buyer-info": {
		editUrl: "buyer/edit",
		submitUrl: "buyer/submit",
		historyDetailUrl: 'buyer/history/detail',
		detailUrl: 'buyer/detail',
		detailField: buyerDetail,
		path: 'buyer'
	},
	"seller-info": {
		editUrl: "seller/edit",
		submitUrl: "seller/submit",
		historyDetailUrl: 'seller/history/detail',
		detailUrl: 'seller/detail',
		detailField: sellerDetail,
		path: 'seller'
	},
	"performance-info": {
		editUrl: "agree/edit",
		submitUrl: "agree/submit",
		historyDetailUrl: 'agree/history/detail',
		detailUrl: 'agree/detail',
		detailField: performanceDetail,
		path: 'agree'
	},
	"invoice-payment": {
		editUrl: "invoice/edit",
		submitUrl: "invoice/submit",
		historyDetailUrl: 'invoice/history/detail',
		detailUrl: 'invoice/detail',
		detailField: invoiceDetail,
		path: 'invoice'
	}
};

export const homeMapping = {
	PUR_MAINTENANCE: ['purchaser/history/detail', purchaseDetail, 'purchase-info'],
	PUR_BID_MAINTENANCE: ['bid/history/detail', subjectDetail, 'subject-info'],
	PUR_SUPPLIER_MAINTENANCE: ['supplier/history/detail', offerDetail, 'offer-supplier'],
	PUR_CONTRA_MAINTENANCE: ['contract/history/detail', contractDetail, 'contract-info'],
	PUR_BUYER_MAINTENANCE: ['buyer/history/detail', buyerDetail, 'buyer-info'],
	PUR_SELLER_MAINTENANCE: ['seller/history/detail', sellerDetail, 'seller-info'],
	PUR_AGREE_MAINTENANCE: ['agree/history/detail', performanceDetail, 'performance-info'],
	PUR_INVO_MAINTENANCE: ['invoice/history/detail', invoiceDetail, 'invoice-payment']
}

export function outLineHandle(router, to) {
	console.log('to', to);
	const path = to.query.type
	const id = to.query.id
	if (to.name === 'detailPage' && (path === 'rule-list' || path === 'rule-base')) {
		router.replace({
			path: path,
			query: {
				id: id
			}
		})
		return;
	}
	if (to.name === 'detailPage') {
		setTimeout(() => {
			superviseInfoAPI.detailPage({
				id: id
			}, mapTable[path].detailUrl).then(res => {
				const detailInfo = res.data
				new Vue.prototype.$sidelipPage(detailPage, {
					props: {
						detailInfo,
						detailField: mapTable[path].detailField
					},
					isNav: false
				})
			})
		}, 300)
		router.replace({
			path: path
		})
		return
	}
}

//解决小数相乘精度问题的方法
export const multiplyAccuracy = (num1,num2) => {
	let m = 0;        //两个数小数点后面的总位数
	let s1 = String(num1);
	let s2 = String(num2);
	try {
		m += s1.split(".")[1].length;
	} 
	catch (e) {
		
	}
	try {
		m += s2.split(".")[1].length;
	}
	catch (e) {
		
	}
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

export function formatNumberWithUnit(num) {
	if (!num) {
		if (num === 0 || num === "0") {
			return 0
		} else {
			return ""
		}
	} else {
		if (num < 1000000) {
			const numUnit = num / 10000;
			const floorNum = Math.floor(multiplyAccuracy(numUnit,100)) / 100;
			return `${floorNum}万`;
		} else if (num >= 1000000 && num < 10000000) {
			const numUnit = num / 1000000;
			const floorNum = Math.floor(multiplyAccuracy(numUnit,100)) / 100;
			return `${floorNum}百万`;
		} else if (num >= 1000000 && num < 100000000) {
			const numUnit = num / 10000000;
			const floorNum = Math.floor(multiplyAccuracy(numUnit,100)) / 100;
			return `${floorNum}千万`;
		} else {
			const numUnit = num / 100000000;
			const floorNum = Math.floor(multiplyAccuracy(numUnit,100)) / 100;
			return `${floorNum}亿`;
		}
	}
}

export function unitToNumber(unit) {
	const strUnit = String(unit).trim()
	const list = [{
			name: '万',
			value: 10000
		},
		{
			name: '百万',
			value: 1000000
		},
		{
			name: '千万',
			value: 10000000
		},
		{
			name: '亿',
			value: 100000000
		}
	]

	for (let i = 0; i < list.length; i++) {
		const idx = strUnit.indexOf(list[i].name)
		if (idx > -1) {
			const num = strUnit.substring(0, idx);
			return Number(list[i].value) * Number(num);
		} else {
			return strUnit
		}
	}
}

export const completionArray = (arr, size, padElement) => {
	while (arr.length < size) {
		arr.push(padElement);
	}
	return arr;
}

// fixNum:保留的小数位数
//小数转为百分比，并去尾法保留几位小数
export const toFixPercent = (num = 0, fixNum = 2) => {
	if (!num) {
		if (num === 0 || num === "0") {
			return 0
		} else {
			return ""
		}
	} else {
		let num1 = multiplyAccuracy(Number(num),100) || 0;
		let fixNum1 = Math.pow(10, Number(fixNum));
		return Math.floor(num1 * fixNum1) / fixNum1 + '%';
	}
}

// fixNum:保留的小数位数
//小数转为百分比,不带百分号，并去尾法保留几位小数
export const toPercentWithoutUnit = (num = 0, fixNum = 2) => {
	if (!num) {
		if (num === 0 || num === "0") {
			return 0
		} else {
			return ""
		}
	} else {
		let num1 = multiplyAccuracy(Number(num),100) || 0;
		let fixNum1 = Math.pow(10, Number(fixNum));
		return Math.floor(num1 * fixNum1) / fixNum1;
	}
}

// 小数转为百分比,不带百分号，进一法法保留几位小数，fixNum:保留的小数位数,
export const toFixedWithNum = (num = 0, fixNum = 2) => {
	if (!num) {
		if (num === 0 || num === "0") {
			return 0
		} else {
			return ""
		}
	} else {
		let num1 = multiplyAccuracy(Number(num),100) || 0;
		let fixNum1 = Math.pow(10, Number(fixNum));
		return Math.floor(num1 * fixNum1) / fixNum1;
	}
}
//保留两位小数
export const toFixedWithSeveral = (num = 0, fixNum = 2) => {
	if (!num) {
		if (num === 0 || num === "0") {
			return 0
		} else {
			return ""
		}
	} else {
		let num1 = Number(num) || 0;
		let fixNum1 = Math.pow(10, Number(fixNum));
		return Math.floor(num1 * fixNum1) / fixNum1;
	}
}

//去掉负数前面的负号,并保留两位小数
export const removeMinusSign = (num, fixNum = 2) => {
	if (!num) {
		if (num === 0 || num === "0") {
			return '-'
		} else {
			return ""
		}
	} else {
		let num1 = num;
		if (String(num1).indexOf("-") > -1) {
			num1 = num.toString().replace(/(^-)/g, "");
		}
		let num2 = multiplyAccuracy(Number(num1),100) || 0;
		let fixNum1 = Math.pow(10, Number(fixNum));
		return Math.floor(num2 * fixNum1) / fixNum1 + '%';
	}
}


//如果是0的话就展示0，其它异常数据，展示/
export const formatAbnormalData = (num) => {
	if (!num) {
		if (num === 0 || num === "0") {
			return 0
		} else {
			return "/"
		}
	} else {
		return num
	}
}

// export const downloadFile = (data) => {
//  const blob = new Blob([data],{type:'application/vnd.ms-excel;charset=utf-8'});
//  const href = window.URL.createObjectURL(blob);
//  const a = document.createElement("a");
//  a.href = href;
//  a.download = "aaa.xlsx";
//  a.style.position = "fixed";
//  a.style.left = "-999px";
//  document.body.appendChild(a);
//  a.click();
//  document.body.removeChild(a);
//  window.URL.revokeObjectURL(href);
// };

export const excelHandle = (res) => {
	const excelName = decodeURI(res.headers["content-disposition"].split(";")[1].split("=")[1]);
	const a = document.createElement("a");
	const blob = new Blob([res.data], {
		type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
	});
	a.href = URL.createObjectURL(blob);
	a.style.display = "none";
	a.download = excelName;
	document.body.appendChild(a);
	a.click();
	document.body.removeChild(a);
}